package www.golf.web.gonwubu;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.python.core.exceptions;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import www.golf.ibatis.bean.Connection;
import www.golf.ibatis.bean.ConnectionShow;
import www.golf.web.session.WebSession;
import www.golf.web.util.UtilListbox;
import www.golf.web.util.UtilZKObject;

public class ConnectionControl extends WebSession{
	UtilZKObject ZKObj =new UtilZKObject(this);
	UtilListbox lb= new UtilListbox();
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	public void initialize() {
		List list=null;
		try {
			list = (List)dbSession().queryForList("Connection.selectConnection");
			dbSessionClose();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		lb.setComponentData(this, "grid", list,dataRender());
	}
	public ListitemRenderer dataRender(){
		 return new ListitemRenderer(){
				public void render(Listitem item, Object data) throws Exception {
					ConnectionShow form = (ConnectionShow) data;
					
					Listcell Serial = new Listcell();
					Serial.setLabel(form.getSerial_number());
					Serial.setParent(item);	
					
					Listcell name = new Listcell();
					name.setLabel(form.getTitle());
					name.setParent(item);		
					
					Listcell type = new Listcell();
					type.setLabel(form.getType());
					type.setParent(item);		
					
					Listcell note = new Listcell();
					note.setLabel(form.getNote());
					note.setParent(item);	
					
					Listcell ismoney = new Listcell();
					ismoney.setLabel(form.isIsmoney()==true?"V":"");
					ismoney.setParent(item);	
					
					Listcell state = new Listcell();
					state.setLabel(form.getData_type());
					state.setParent(item);	
					
					Listcell personal = new Listcell();
					personal.setLabel(form.getAccount_name());
					personal.setParent(item);	
					
					Listcell c_date = new Listcell();
					c_date.setLabel(setDate(form.getCreate_date()));
					c_date.setParent(item);	
					
					Listcell pid = new Listcell();
					pid.setLabel(String.valueOf(form.getPid()));
					pid.setParent(item);
					pid.setVisible(false);
				}		
			};
	}
	public void EditPage(String key,String open_page,String win_title){
		Map<String, String> s_map=new HashMap<String, String>();
		s_map.put("pid", key);
		s_map.put("data_type", String.valueOf(getData_type()));
		Window win=(Window)Executions.createComponents(getPagefileSession()+open_page, this, s_map);
		win.setTitle(win_title);
		try {
			win.doModal();
		} catch (InterruptedException e){
			e.printStackTrace();
		}
	}
	public void onEdit(){
		String value=lb.getPKListcellValue(this, "grid");
		EditPage(value, "edit.zul", "修改联系单");
		
	}
	public void check(){
		setRowData();
		String value=lb.getPKListcellValue(this, "grid");
		if(value!=null&&setEditPower()&&getData_type()<=2){
			getFellow("p_w_edit").setVisible(true);
			if(getData_type()==2){getFellow("p_w_put").setVisible(true);}
			else{getFellow("p_w_put").setVisible(false);};
		}else if(value!=null&&setEditPower()&&getData_type()==3){
			getFellow("p_w_edit").setVisible(true);
			getFellow("p_w_put").setVisible(true);
		}else if(!setEditPower()&&getData_type()<=2&&!checkJob(100)){
			getFellow("p_w_edit").setVisible(false);
			getFellow("p_w_put").setVisible(false);
		}else if(!setEditPower()&&getData_type()==2&&checkJob(100)){
			getFellow("p_w_edit").setVisible(false);
			getFellow("p_w_put").setVisible(true);
		}
	}
	public void onPut(){
		String value=lb.getPKListcellValue(this, "grid");
		EditPage(value, "final.zul", "归挡资料");
	}
	public boolean checkJob(int job){
		for(int i=0;i<getUserJob().length;i++){
			if(getUserJob()[i]==job)return true;
		}
		return false;
	}
	public void onDelete(){
		String value=lb.getPKListcellValue(this, "grid");
		if(value!=null){
			int key =Integer.parseInt(value);
			try {
				if(Messagebox.show("确定删除?", "注意", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION )==Messagebox.YES){
					dbSession().delete("Connection.deleteConnection",key);
					initialize();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public boolean setEditPower(){
		if(getData_type()>1){
			return (getUserPower()<5)?true:false;
		}else{
			return true;
		}
	}
	public int getData_type(){
		try {
			return (Integer) dbSession().queryForObject("Connection.getConnectionDataType",Integer.parseInt(lb.getPKListcellValue(this, "grid")));
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return -1;
	}
	public void onAdd() throws InterruptedException{
		Window win=(Window)Executions.createComponents(getPagefileSession()+"edit.zul", null, null);
		win.setTitle("新增联系单");
		win.doModal();
	}
	/*public void setListProsetData(Window win,int value){
		((Listbox)win.getFellow("ed_projectset")).setDisabled(false);
		setList(win,"Projectset.select_List","ed_projectset",value);
	}
	public void setListData(Window win){
		setList(win,"Project.select_List","ed_project");
		setList(win,"Agreement_Type.select_List","ed_type");
		setList(win,"Subcontractor.select_List","ed_subcontractor");
		setList(win,"User.select_List","ed_signer");
	}*/
	public String setDate(Date date){
		return date!=null?sdf.format(date):null;
	}
	public void setRowData(){
		String value=lb.getPKListcellValue(this, "grid");
		//------
		ConnectionShow bean=new ConnectionShow();
		List con_list=null;
		List supervision_list=null;
		List agrees_list=null;
		String s_l="";
		String a_l="";
		try {
			 bean = (ConnectionShow)dbSession().queryForObject("Connection.getConnection",Integer.parseInt(value));
			 supervision_list=(List)dbSession().queryForList("Connection.getCon_Supervision_list",Integer.parseInt(value));
			 agrees_list=(List)dbSession().queryForList("Connection.getCon_Agrees_list",Integer.parseInt(value));
			 dbSessionClose();
		} catch (Exception e) {
			e.printStackTrace();
		}
		for(Object obj:supervision_list){
			s_l+=((Map)obj).get("s_name")+",";
		}
		for(Object obj:agrees_list){
			a_l+=((Map)obj).get("a_name")+",";
		}
		s_l=s_l.substring(0,(s_l.length()==0)?0:s_l.length()-1);
		a_l=a_l.substring(0,(a_l.length()==0)?0:a_l.length()-1);
		ZKObj.setObjectValue("p_w_serial", bean.getSerial_number());
		ZKObj.setObjectValue("p_w_title", bean.getTitle());
		ZKObj.setObjectValue("p_w_note", bean.getNote());
		ZKObj.setObjectValue("p_w_content", bean.getContent());
		ZKObj.setObjectValue("p_w_type", bean.getType());
		ZKObj.setObjectValue("p_w_data_type", bean.getData_type());
		ZKObj.setObjectValue("p_w_person", bean.getAccount_name());
		ZKObj.setObjectValue("p_w_money1", bean.getMoney());
		ZKObj.setObjectValue("p_w_money2", bean.getMoney2());
		ZKObj.setObjectValue("p_w_ismoney", bean.isIsmoney());
		ZKObj.setObjectValue("p_w_Cdate", bean.getCreate_date());
		ZKObj.setObjectValue("p_w_Edate", bean.getEnd_date());
		ZKObj.setObjectValue("p_w_uion", a_l);
		ZKObj.setObjectValue("p_w_supervision", "".equals(s_l)?"无":s_l);
		if(getData_type()<=2){
			getFellow("download").setVisible(false);
		}else if(getData_type()==3){
			getFellow("download").setVisible(true);
		}
	}
	public void download() throws Exception{
		String value=lb.getPKListcellValue(this, "grid");
		Map map=(Map)dbSession().queryForObject("Connection.getConnectionRAR",Integer.parseInt(value));
		byte[] data=(byte[])map.get("rar_data");
		InputStream ins = new ByteArrayInputStream(data);
		Filedownload.save(ins, "rar", ZKObj.getValue("p_w_serial")+".rar");
	}
}
